﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using DevExpress.XtraTreeList.Nodes;

namespace TS_XY_Train_Repaire.UserCon.SysSetting
{
    public partial class uc_Sys_Settings_Level_Auth_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID = "";
        public string _LoadName = "";
        public uc_Sys_Settings_Level_Auth_ManaForm()
        {
            InitializeComponent();
        }

        private void uc_Sys_Settings_Level_Auth_ManaForm_Load(object sender, EventArgs e)
        {
            this.Text = _LoadName+"->>授权管理";
            _LoadOrgList();
            _LoadAuthGrid();
        }
        #region//--组织结构--//
        private void _LoadOrgList()
        {
            LeverString_SysOrganization _ls = new LeverString_SysOrganization();
            tl_Main.Nodes.Clear();
            tl_Main.Refresh();
            ArrayList pList = new ArrayList();
            string strSQL = "select *,(select count(*) from TS_S_T_S_Tree_Authority where f_menuid=" + _LoadID.Substring(1) + " and f_type=2 and f_funcid=TS_S_T_P_Department.f_id and f_del=0) as f_count from TS_S_T_P_Department where f_rsid=" + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations + " and f_del=0 and f_deep=0 order by f_id asc";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            string _strDepQuery = " and (";
            int i = 0;
            while (read.Read())
            {
                if (i == 0)
                {
                    _strDepQuery += "(f_level like ',%" + read["f_id"].ToString() + "%,')";
                }
                else
                {
                    _strDepQuery += " or (f_level like ',%" + read["f_id"].ToString() + "%,')";
                }
                _ls = new LeverString_SysOrganization();
                _ls.f_id = "D" + read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_deep = read["f_deep"].ToString();
                _ls.f_level = read["f_level"].ToString();
                _ls.f_rbid = read["f_rbid"].ToString();
                _ls.f_rsid = read["f_rsid"].ToString();
                _ls.f_depid = read["f_id"].ToString();
                _ls.f_imgindex = 3;
                if (read["f_count"].ToString() == "0")
                {
                    _ls.f_ck = false;
                }
                else
                {
                    _ls.f_ck = true;
                }
                pList.Add(_ls);
                i++;
            }
            _strDepQuery += ")";
            read.Close();
            strSQL = "select *,(select count(*) from TS_S_T_S_Tree_Authority where f_menuid=" + _LoadID.Substring(1) + " and f_type=2 and f_funcid=TS_S_T_P_Department.f_id and f_del=0) as f_count from TS_S_T_P_Department where f_rsid=" + TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations + " " + _strDepQuery + " and f_deep>0 and f_del=0 order by f_id asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _ls = new LeverString_SysOrganization();
                _ls.f_id = "D" + read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_deep = read["f_deep"].ToString();
                _ls.f_level = read["f_level"].ToString();
                _ls.f_rbid = read["f_rbid"].ToString();
                _ls.f_rsid = read["f_rsid"].ToString();
                _ls.f_depid = read["f_id"].ToString();
                string _UPID = "";
                _UPID = read["f_level"].ToString();
                _UPID = _UPID.Substring(0, _UPID.Length - 1);
                _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                _ls.f_parent = "D" + _UPID;
                _ls.f_imgindex = 3;
                if (read["f_count"].ToString() == "0")
                {
                    _ls.f_ck = false;
                }
                else
                {
                    _ls.f_ck = true;
                }
                pList.Add(_ls);
            }
            read.Close();
            strSQL = "select f_id,(select f_name from TS_S_T_P_Position where f_id=TS_S_T_P_Dep_Position.f_posid)as f_name,(select f_code from TS_S_T_P_Position where f_id=TS_S_T_P_Dep_Position.f_posid)as f_code,f_depid,f_posid,(select f_rbid from TS_S_T_P_Department where f_id=TS_S_T_P_Dep_Position.f_depid) as f_rbid,(select f_rsid from TS_S_T_P_Department where f_id=TS_S_T_P_Dep_Position.f_depid) as f_rsid,(select count(*) from TS_S_T_S_Tree_Authority where f_menuid=" + _LoadID.Substring(1) + " and f_type=1 and f_funcid=TS_S_T_P_Dep_Position.f_posid and f_del=0) as f_count from TS_S_T_P_Dep_Position where f_del=0 and f_depid in (select f_id from TS_S_T_P_Department where f_del=0 ) and f_posid in (select f_id from TS_S_T_P_Position where f_del=0 ) order by f_id asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _ls = new LeverString_SysOrganization();
                _ls.f_id = "P" + read["f_posid"].ToString() + "D" + read["f_depid"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_code = read["f_code"].ToString();
                _ls.f_rbid = read["f_rbid"].ToString();
                _ls.f_rsid = read["f_rsid"].ToString();
                _ls.f_depid = read["f_depid"].ToString();
                _ls.f_posid = read["f_posid"].ToString();
                _ls.f_parent = "D" + read["f_depid"].ToString();
                _ls.f_imgindex = 4;
                if (read["f_count"].ToString() == "0")
                {
                    _ls.f_ck = false;
                }
                else
                {
                    _ls.f_ck = true;
                }
                pList.Add(_ls);
            }
            read.Close();
            strSQL = "select f_id,f_name,f_depid,f_posid,f_rbid,f_rsid,(select count(*) from TS_S_T_S_Tree_Authority where f_menuid=" + _LoadID.Substring(1) + " and f_type=0 and f_funcid=TS_S_T_P_Stuff.f_id and f_del=0) as f_count from TS_S_T_P_Stuff where f_del=0 and f_rsid="+TS_Win_Locomotive_MainForm._SysSettings._GetRailwayStations+" order by f_id asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _ls = new LeverString_SysOrganization();
                _ls.f_id = "S" + read["f_id"].ToString();
                _ls.f_name = read["f_name"].ToString();
                _ls.f_rbid = read["f_rbid"].ToString();
                _ls.f_rsid = read["f_rsid"].ToString();
                _ls.f_depid = read["f_depid"].ToString();
                _ls.f_parent = "P" + read["f_posid"].ToString() + "D" + read["f_depid"].ToString();
                _ls.f_imgindex = 5;
                if (read["f_count"].ToString() == "0")
                {
                    _ls.f_ck = false;
                }
                else
                {
                    _ls.f_ck = true;
                }
                pList.Add(_ls);
            }
            read.Close();
            conn.Close();
            conn.Dispose();

            tl_Main.DataSource = pList;
        }
        #endregion
        private void _LoadAuthGrid()
        {
            string strSQL = "select row_number() over (order by f_id) as f_row,*,(case f_type when 0 then '个人' when 1 then '职务' when 2 then '部门' end) as f_typename,(case f_type when 0 then (select f_name from TS_S_T_P_Stuff where f_id=TS_S_T_S_Tree_Authority.f_funcid)when 1 then (select f_name from TS_S_T_P_Position where f_id=TS_S_T_S_Tree_Authority.f_funcid)when 2 then (select f_name from TS_S_T_P_Department where f_id=TS_S_T_S_Tree_Authority.f_funcid) end) as f_funcname,convert(bit,0) as f_ck from TS_S_T_S_Tree_Authority where f_menuid=" + _LoadID.Substring(1) + " and f_del=0";
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            
            gc_Main.DataSource = dt;

        }
        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string _FuncList = GetFunclIst(tl_Main.Nodes, "");
            TS_Win_Class.MySqlCom("insert into TS_S_T_H_Tree_Authority select * from TS_S_T_S_Tree_Authority where f_menuid="+_LoadID.Substring(1));
            TS_Win_Class.MySqlCom("delete TS_S_T_S_Tree_Authority where f_menuid=" + _LoadID.Substring(1));
            if (_FuncList.Length > 0)
            {
                string[] _arry = _FuncList.Split(',');
                for (int i = 0; i < _arry.Length; i++)
                {
                    if (_arry[i].Length > 0)
                    {
                        switch (_arry[i].Substring(0, 1))
                        {
                            case "D":
                                strSQL += "insert into TS_S_T_S_Tree_Authority(f_menuid,f_type,f_funcid,f_workerid) values(" + _LoadID.Substring(1) + ",2," + _arry[i].Substring(1) + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ");";
                                break;
                            case "S":
                                strSQL += "insert into TS_S_T_S_Tree_Authority(f_menuid,f_type,f_funcid,f_workerid) values(" + _LoadID.Substring(1) + ",0," + _arry[i].Substring(1) + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ");";
                                break;
                            case "P":
                                strSQL += "insert into TS_S_T_S_Tree_Authority(f_menuid,f_type,f_funcid,f_workerid) values(" + _LoadID.Substring(1) + ",1," + _arry[i].Substring(1, _arry[i].IndexOf('D')-1) + "," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ");";
                                break;
                        }
                    }
                }
                TS_Win_Class.MySqlCom(strSQL);
                _LoadAuthGrid();
            }
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string _str = "";
            for (int i = 0; i < gv_Main.DataRowCount; i++)
            {
                if ((bool)gv_Main.GetRowCellValue(i, "f_ck"))
                {
                    _str += gv_Main.GetRowCellValue(i, "f_id").ToString()+",";
                }
            }
            _str = _str.Substring(0, _str.Length-1);
            TS_Win_Class.MySqlCom("insert into TS_S_T_H_Tree_Authority select * from TS_S_T_S_Tree_Authority where f_id in("+_str+")");
            TS_Win_Class.MySqlCom("delete TS_S_T_S_Tree_Authority where  f_id in(" + _str + ")");
            _LoadOrgList();
            _LoadAuthGrid();
        }
        private string GetFunclIst(TreeListNodes _Nodes, string _str)
        {
            string _Str = _str;
            foreach (TreeListNode node in _Nodes)
            {
                if ((bool)node.GetValue("f_ck"))
                {
                    _Str += node.GetValue("f_id").ToString() + ",";
                }
                if (node.HasChildren)
                {
                    _Str = GetFunclIst(node.Nodes, _Str);
                }
            }
            return _Str;
        }

        private void uc_Sys_Settings_Level_Auth_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}